package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.dao.CashDao;
import com.baichang.taoshunda.entity.Cash;
import org.springframework.stereotype.Repository;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Repository
public class CashDaoImpl extends BaseDaoImpl<Cash> implements CashDao {

    /**
     * 根据商家编号获取商家提现记录
     *
     * @param bussinessId 商家编号
     * @return
     */
    @Override
    public List<Map<String, Object>> findCashHistoryByBussinessId(Integer bussinessId) {
        String sql = " SELECT * FROM {table} ca INNER JOIN t_user_bank ub ON ca.bankcard_id=ub.id WHERE company_id=? AND ub.state=1 ORDER BY ca.created DESC ";
        return queryForMapList(sql, bussinessId);
    }

    /**
     * 查看商家提现列表
     *
     * @param bussId 商家编号
     * @return
     */
    @Override
    public List<Map<String, Object>> findCashList(Integer bussId) {
        String sql = " SELECT * FROM {table} WHERE company_id=? ORDER BY created DESC ";
        return queryForMapList(sql, bussId);
    }

    /**
     * 商家提现列表  busscashQuery
     */
    @Override
    public Page<Map<String, Object>> busscashQuery(int nowPage, int pageSize, String bussName, String startTime, String endTime, int cashstatus, int companyId) {
        StringBuffer stringBuffer = new StringBuffer();
        List<Object> param = new ArrayList<Object>();
        stringBuffer.append("SELECT a.*,tb.buss_name AS bName FROM t_cash a LEFT JOIN t_bussiness tb ON tb.id=a.company_id WHERE a.company_id IS NOT NULL ");
        if (StringUtils.isNotBlank(bussName)) {
            stringBuffer.append(" and tb.buss_name like ?");
            param.add("%" + bussName + "%");
        }
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            stringBuffer.append(" and date_format(a.created,'%Y-%m-%d') between ? and ?");
            param.add(startTime);
            param.add(endTime);
        }
        if (cashstatus > 0) {
            stringBuffer.append(" and a.state=?");
            param.add(cashstatus);
        }
        if (companyId > 0) {
            stringBuffer.append(" and a.company_id=?");
            param.add(companyId);
        }
        stringBuffer.append(" ORDER BY a.created DESC,a.state ASC");
        return pageForMapList(stringBuffer.toString(), nowPage, pageSize, param.toArray());
    }


    /**
     * 商家提现列表的导出  busscashQueryExport
     */
    @Override
    public List<Map<String, Object>> busscashQueryExport(String ids) {
        StringBuffer stringBuffer = new StringBuffer();
        List<Object> param = new ArrayList<Object>();

        stringBuffer.append("SELECT *,a.id,b.card_name as cardName,a.created as created,CASE a.state when 1 then '待审核' " +
                "when 2 then '审核通过' when 3 then '审核拒绝' end as state,a.name as name FROM t_cash a left join t_bankcard_type b on a.bankcard_id=b.id WHERE a.id in (" + ids + ")");

        return queryForMapList(stringBuffer.toString(), param.toArray());
    }

    /**
     * 提现的审核通过与审核失败
     */
    @Override
    public boolean audit(int id, int state, String auditPersonName, String auditReason) {
        if (!auditReason.equals("")) {
            String sql = "update t_cash a set a.state=?,a.deal_name=?,a.reason=?,a.deal_time=? where a.id=? ";
            return update(sql, state, auditPersonName, auditReason, new Date(), id) > 0;
        } else {
            String sql = "update t_cash a set a.state=?,a.deal_name=?,a.deal_time=? where a.id=? ";
            return update(sql, state, auditPersonName, new Date(), id) > 0;
        }
    }

    /**
     * 配送员提现列表
     */
    @Override
    public Page<Map<String, Object>> delivercashQuery(int nowPage, int pageSize, String bussName, String startTime, String endTime, int cashstatus, int companyId) {

        StringBuffer stringBuffer = new StringBuffer();
        List<Object> param = new ArrayList<Object>();
        stringBuffer.append("SELECT a.*,dc.name AS dName FROM t_cash a LEFT JOIN t_delivery_clerk dc ON dc.id=a.delivery_id WHERE a.delivery_id IS NOT NULL ");
        if (StringUtils.isNotBlank(bussName)) {
            stringBuffer.append(" and a.name like ?");
            param.add("%" + bussName + "%");
        }
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            stringBuffer.append(" and date_format(a.created,'%Y-%m-%d') between ? and ?");
            param.add(startTime);
            param.add(endTime);
        }

        if (cashstatus > 0) {
            stringBuffer.append(" and a.state=?");
            param.add(cashstatus);
        }
        if (companyId > 0) {
            stringBuffer.append(" and a.delivery_id=?");
            param.add(companyId);
        }
        stringBuffer.append(" ORDER BY a.created DESC,a.state ASC");
        return pageForMapList(stringBuffer.toString(), nowPage, pageSize, param.toArray());
    }


    /**
     * 配送员提现列表的导出
     */
    @Override
    public List<Map<String, Object>> delivercashQueryExport(String ids) {
        StringBuffer stringBuffer = new StringBuffer();
        List<Object> param = new ArrayList<Object>();

        stringBuffer.append("SELECT *,a.id,b.card_name as cardName,a.created as created,CASE a.state when 1 then '待审核' " +
                "when 2 then '审核通过' when 3 then '审核拒绝' end as state,a.name as name FROM t_cash a left join t_bankcard_type b on a.bankcard_id=b.id WHERE a.id in (" + ids + ")");

        return queryForMapList(stringBuffer.toString(), param.toArray());
    }

    /**
     * 提现列表的查询  indexQuery
     */
    @Override
    public Page<Map<String, Object>> indexQuery(int nowPage, int pageSize, String bussName, int cashstatus, String startTime, String endTime, String checkBusinessName, String checkDeliveryName) {
        StringBuffer stringBuffer = new StringBuffer();
        List<Object> param = new ArrayList<Object>();
        stringBuffer.append("SELECT a.*,tb.buss_name AS bName,dc.name AS dName FROM t_cash a LEFT JOIN t_bussiness tb ON tb.id=a.company_id LEFT JOIN t_delivery_clerk dc ON dc.id=a.delivery_id WHERE 1=1 ");
        if (StringUtils.isNotBlank(bussName)) {
            stringBuffer.append(" and a.name like ?");
            param.add("%" + bussName + "%");
        }
        if (StringUtils.isNotBlank(checkBusinessName)) {
            stringBuffer.append(" and tb.buss_name like ?");
            param.add("%" + checkBusinessName + "%");
        }
        if (StringUtils.isNotBlank(checkDeliveryName)) {
            stringBuffer.append(" and dc.name like ?");
            param.add("%" + checkDeliveryName + "%");
        }

        if (cashstatus > 0) {
            stringBuffer.append(" and a.state=?");
            param.add(cashstatus);
        }
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            stringBuffer.append(" and date_format(a.created,'%Y-%m-%d') between ? and ?");
            param.add(startTime);
            param.add(endTime);
        }

        stringBuffer.append(" ORDER BY a.created DESC,a.state ASC");

        return pageForMapList(stringBuffer.toString(), nowPage, pageSize, param.toArray());
    }

    @Override
    public List<Map<String, Object>> findCashHistoryByDisId(Integer disId) {
        String sql = "SELECT * FROM {table} WHERE delivery_id=? ORDER BY created DESC ";
        return queryForMapList(sql, disId);
    }

    @Override
    public List<Map<String, Object>> findDeliveryCashList(Integer disId, Integer nowPage, int pageSize) {
        String sql = "SELECT * FROM {table} WHERE delivery_id=? ORDER BY created DESC";
        return listForMapList(sql, nowPage, pageSize, disId);
    }

    @Override
    public int getCountByCompanyId(Integer bussId) {

        String sql = "select count(0) from {table} where company_id =? AND TO_DAYS(created)=TO_DAYS(NOW())";
        return queryForInt(sql, bussId);
    }

    @Override
    public List<Map<String, Object>> findCashHistoryByUserId(Integer userId) {
        String sql = "SELECT * FROM {table} WHERE user_id=? ORDER BY created DESC ";
        return queryForMapList(sql, userId);
    }

    @Override
    public Integer getCountByUserId(Integer userId) {
        String sql = "select count(0) from {table} where user_id =? AND TO_DAYS(created)=TO_DAYS(NOW())";
        return queryForInt(sql, userId);
    }
}
